//
//  SecondViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "SecondViewController.h"
#import "GetDataMethods.h"
#import "TripItems.h"
#import "TripItemTableViewCell.h"
#import "DownDetailsViewController.h"
#import "Model.h"
#import "Fit.h"
#import "MBProgressHUD.h"

@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArray;
@property(nonatomic, retain)MBProgressHUD *progressView;

@end

@implementation SecondViewController
-(void)dealloc
{
    [_tableView release];
    [_dataArray release];
    [super dealloc];
}

-(void)changeColor{
    self.tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataArray = [NSMutableArray array];
    [self changeColor];
    [self getData];
    
}
-(void)getData
{
    NSArray *array = [_string componentsSeparatedByString:@"/"];
    [GetDataMethods getDataByString:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/trips/?start=0&count=20", array[2], array[3]] BodyString:nil WithDataBlock:^(id data) {
        NSArray *itemArray = [data objectForKey:@"items"];
        for (NSDictionary *dic in itemArray) {
            TripItems *item = [[TripItems alloc]init];
            [item setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:item];

        }
        [_tableView reloadData];
        [_progressView removeFromSuperview];
    }];
}

-(void)loadView
{
    [super loadView];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    //初始化加载中视图
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"玩命加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
    [self.view addSubview:_progressView];
    [_progressView release];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    TripItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[TripItemTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count != 0) {
        cell.trip = _dataArray[indexPath.row];
        cell.backgroundColor = [Model shareInstance].BackgroundColor;
    }
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150 * H;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DownDetailsViewController *downVC = [[DownDetailsViewController alloc]init];
    if (_dataArray.count != 0) {
        downVC.string = [_dataArray[indexPath.row] Id];
    }
    [self.navigationController pushViewController:downVC animated:YES];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
